// weapp/fields_editor/select/select.js
import ex from '../../ex/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    head:{
      type:Object,
    },
    value:{
      type: String,
      value:'',
      optionalTypes:[Number]
    },
    required:{
      type:Boolean,
      value:false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    options:[],
    // show:false,
    show_label:'请选择',
    index:0, // 正常的index是从1开始，0代表什么都没选
  },
  observers:{
    options(nv){
      this.clear()
    },
    value(nv){
      var one = ex.findone(this.data.options,{value:nv})
      if(one){
        var index = this.data.options.indexOf(one)
        this.setData({
          show_label:one.label,
          index:index
        })
      } else{
        this.setData({
          show_label:nv,
          index:0
        })
      }
      if(!nv){
        this.setData({
          show_label:'请选择'
        })
      }
     
    },
  },
  attached () {
    if(this.properties.head.options){
      this.setData({
        options:this.properties.head.options
      })
    }

      // this.properties.options.forEach(item => {
      //   item.name=item.label
      // });
   },
  /**
   * 组件的方法列表
   */
  methods: {
    clear(){
      this.setData({
        show_label:'请选择',
        index:0
      })

      this.triggerEvent('input',null)
    },
    bindPickerChange(event){
      var selected =  this.data.options[event.detail.value]
      this.triggerEvent('input', selected.value)
      if(this.properties.head.onChange){
        this.properties.head.onChange(selected.value)
      }
    },
    // popSelect(){
    //   this.setData({
    //     show:true,
    //   })
    //   console.log('打开')
    // },
    // onSelect(event){
    //   console.log(event.detail);
    //   this.triggerEvent('input', event.detail.value)
    //     console.log('选择')
    // },
    // onClose(){
    //   this.setData({
    //     show:false,
    //   })
    // },
  }
})
